<!DOCTYPE html>
<html>

<head>
  <title>Quarkus - Google Cloud Functions (Serverless)</title>
  <script id="adobe_dtm" src="https://www.redhat.com/dtm.js" type="text/javascript"></script>
  <script src="/assets/javascript/highlight.pack.js" type="text/javascript"></script>
  <META HTTP-EQUIV='Content-Security-Policy' CONTENT="default-src 'none'; script-src 'self' 'unsafe-eval' 'sha256-ANpuoVzuSex6VhqpYgsG25OHWVA1I+F6aGU04LoI+5s=' 'sha256-ipy9P/3rZZW06mTLAR0EnXvxSNcnfSDPLDuh3kzbB1w=' js.bizographics.com https://www.redhat.com assets.adobedtm.com jsonip.com https://ajax.googleapis.com https://www.googletagmanager.com https://www.google-analytics.com https://use.fontawesome.com; style-src 'self' https://fonts.googleapis.com https://use.fontawesome.com; img-src 'self' *; media-src 'self' ; frame-src https://www.googletagmanager.com https://www.youtube.com; frame-ancestors 'none'; base-uri 'none'; object-src 'none'; form-action 'none'; font-src 'self' https://use.fontawesome.com https://fonts.gstatic.com;">
  <META HTTP-EQUIV='X-Frame-Options' CONTENT="DENY">
  <META HTTP-EQUIV='X-XSS-Protection' CONTENT="1; mode=block">
  <META HTTP-EQUIV='X-Content-Type-Options' CONTENT="nosniff">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="Quarkus: Supersonic Subatomic Java">
  <meta name="twitter:card" content="summary_large_image">
  <meta name="twitter:site" content="@QuarkusIO"> 
  <meta name="twitter:creator" content="@QuarkusIO">
  <meta property="og:url" content="https://quarkus.io/guides/gcp-functions" />
  <meta property="og:title" content="Quarkus - Google Cloud Functions (Serverless)" />
  <meta property="og:description" content="Quarkus: Supersonic Subatomic Java" />
  <meta property="og:image" content="/assets/images/quarkus_card.png" />
  <link rel="canonical" href="https://quarkus.io/guides/gcp-functions">
  <link rel="shortcut icon" type="image/png" href="/favicon.ico" >
  <link rel="stylesheet" href="https://quarkus.io/guides/stylesheet/config.css" />
  <link rel="stylesheet" href="/assets/css/main.css" />
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">
  <link rel="alternate" type="application/rss+xml"  href="https://quarkus.io/feed.xml" title="Quarkus">
  <script src="https://quarkus.io/assets/javascript/goan.js" type="text/javascript"></script>
  <script src="https://quarkus.io/assets/javascript/hl.js" type="text/javascript"></script>
</head>

<body class="guides">
  <!-- Google Tag Manager (noscript) -->
  <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-NJWS5L"
  height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
  <!-- End Google Tag Manager (noscript) -->

  <div class="nav-wrapper">
  <div class="grid-wrapper">
    <div class="width-12-12">
      <input type="checkbox" id="checkbox" />
      <nav id="main-nav" class="main-nav">
  <div class="container">
    <div class="logo-wrapper">
      
        <a href="/"><img src="/assets/images/quarkus_logo_horizontal_rgb_600px_reverse.png" class="project-logo" title="Quarkus"></a>
      
    </div>
    <label class="nav-toggle" for="checkbox">
      <i class="fa fa-bars"></i>
    </label>
    <div id="menu" class="menu">
      <span>
        <a href="/get-started/" class="">Get Started</a>
      </span>
      <span>
        <a href="/guides/" class="active">Guides</a>
      </span>
      <span>
        <a href="/community/" class="">Community</a>
      </span>
      <span>
        <a href="/support/" class="">Support</a>
      </span>
      <span>
        <a href="/blog/" class="">Blog</a>
      </span>
      <span>
        <a href="https://code.quarkus.io" class="button-cta secondary white">Start Coding</a>
      </span>
    </div>
  </div>
      </nav>
    </div>
  </div>
</div>

  <div class="content">
    <div class="guide">
  <div class="width-12-12">
    <h1 class="text-caps">Quarkus - Google Cloud Functions (Serverless)</h1>
    <div class="hide-mobile toc"><ul class="sectlevel1">
<li><a href="#prerequisites">Prerequisites</a></li>
<li><a href="#solution">Solution</a></li>
<li><a href="#creating-the-maven-deployment-project">Creating the Maven Deployment Project</a></li>
<li><a href="#login-to-google-cloud">Login to Google Cloud</a></li>
<li><a href="#creating-the-functions">Creating the functions</a></li>
<li><a href="#choose-your-function">Choose Your Function</a>
<ul class="sectlevel2">
<li><a href="#the-httpfunction">The HttpFunction</a></li>
<li><a href="#the-backgroundfunction">The BackgroundFunction</a></li>
<li><a href="#the-rawbackgroundfunction">The RawBackgroundFunction</a></li>
</ul>
</li>
<li><a href="#build-and-deploy-to-google-cloud">Build and Deploy to Google Cloud</a>
<ul class="sectlevel2">
<li><a href="#the-httpfunction-2">The HttpFunction</a></li>
<li><a href="#the-backgroundfunction-2">The BackgroundFunction</a></li>
<li><a href="#the-rawbackgroundfunction-2">The RawBackgroundFunction</a></li>
</ul>
</li>
<li><a href="#testing-locally">Testing locally</a>
<ul class="sectlevel2">
<li><a href="#the-httpfunction-3">The HttpFunction</a></li>
<li><a href="#the-backgroundfunction-3">The BackgroundFunction</a></li>
<li><a href="#the-rawbackgroundfunction-3">The RawBackgroundFunction</a></li>
</ul>
</li>
</ul></div>
    <div>
      <div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>The <code>quarkus-google-cloud-functions</code> extension allows you to use Quarkus to build your Google Cloud Functions.
Your functions can use injection annotations from CDI or Spring and other Quarkus facilities as you need them.</p>
</div>
<div class="paragraph">
<p>As the Google Cloud Function Java engine is a new Beta feature of Google Cloud, this extension is flagged as experimental.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<div class="paragraph">
<p>This technology is considered experimental.</p>
</div>
<div class="paragraph">
<p>In <em>experimental</em> mode, early feedback is requested to mature the idea.
There is no guarantee of stability nor long term presence in the platform until the solution matures.
Feedback is welcome on our <a href="https://groups.google.com/d/forum/quarkus-dev">mailing list</a> or as issues in our <a href="https://github.com/quarkusio/quarkus/issues">GitHub issue tracker</a>.</p>
</div>
<div class="paragraph">
<p>For a full list of possible extension statuses, check our <a href="https://quarkus.io/faq/#extension-status">FAQ entry</a>.</p>
</div>
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect1">
<h2 id="prerequisites"><a class="anchor" href="#prerequisites"></a>Prerequisites</h2>
<div class="sectionbody">
<div class="paragraph">
<p>To complete this guide, you need:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>less than 15 minutes</p>
</li>
<li>
<p>JDK 11 (Google Cloud Functions requires JDK 11)</p>
</li>
<li>
<p>Apache Maven 3.6.2+</p>
</li>
<li>
<p><a href="https://cloud.google.com/">A Google Cloud Account</a>.  Free accounts work.</p>
</li>
<li>
<p><a href="https://cloud.google.com/sdk">Cloud SDK CLI Installed</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="solution"><a class="anchor" href="#solution"></a>Solution</h2>
<div class="sectionbody">
<div class="paragraph">
<p>This guide walks you through generating a sample project followed by creating multiple functions showing how to implement <code>HttpFunction</code>, <code>BackgroundFunction</code> and <code>RawBackgroundFunction</code> in Quarkus.
Once built, you will be able to deploy the project to Google Cloud.</p>
</div>
<div class="paragraph">
<p>If you don&#8217;t want to follow all these steps, you can go right to the completed example.</p>
</div>
<div class="paragraph">
<p>Clone the Git repository: <code>git clone <a href="https://github.com/quarkusio/quarkus-quickstarts.git" class="bare">https://github.com/quarkusio/quarkus-quickstarts.git</a></code>, or download an <a href="https://github.com/quarkusio/quarkus-quickstarts/archive/master.zip">archive</a>.</p>
</div>
<div class="paragraph">
<p>The solution is located in the <code>google-cloud-functions-quickstart</code> <a href="https://github.com/quarkusio/quarkus-quickstarts/tree/master/google-cloud-functions-quickstart">directory</a>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="creating-the-maven-deployment-project"><a class="anchor" href="#creating-the-maven-deployment-project"></a>Creating the Maven Deployment Project</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Create an application with the <code>quarkus-google-cloud-functions</code> extension.
You can use the following Maven command to create it:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code data-lang="shell" class="language-shell hljs">mvn io.quarkus:quarkus-maven-plugin:1.7.0.Final:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=google-cloud-functions \
    -DclassName="org.acme.quickstart.GreetingResource" \
    -Dpath="/hello" \
    -Dextensions="google-cloud-functions"</code></pre>
</div>
</div>
<div class="paragraph">
<p>Now, let&#8217;s remove what&#8217;s not needed inside the generated application:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Remove the dependency <code>io.quarkus:quarkus-reasteasy</code> from your <code>pom.xml</code> file.</p>
</li>
<li>
<p>Remove the generated <code>org.acme.quickstart.GreetingResource</code> class.</p>
</li>
<li>
<p>Remove the <code>index.html</code> from <code>resources/META-INF/resources</code> or it will be picked up instead of your Function.</p>
</li>
<li>
<p>Remove the existing tests.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="login-to-google-cloud"><a class="anchor" href="#login-to-google-cloud"></a>Login to Google Cloud</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Login to Google Cloud is necessary for deploying the application and it can be done as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">gcloud auth login</code></pre>
</div>
</div>
<div class="paragraph">
<p>At the time of this writing, Cloud Functions are still in beta so make sure to install the <code>beta</code> command group.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">gcloud components install beta</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="creating-the-functions"><a class="anchor" href="#creating-the-functions"></a>Creating the functions</h2>
<div class="sectionbody">
<div class="paragraph">
<p>For this example project, we will create three functions, one <code>HttpFunction</code>, one <code>BackgroundFunction</code> (Storage event) and one <code>RawBakgroundFunction</code> (PubSub event).</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="choose-your-function"><a class="anchor" href="#choose-your-function"></a>Choose Your Function</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The <code>quarkus-google-cloud-functions</code> extension scans your project for a class that directly implements the Google Cloud <code>HttpFunction</code>, <code>BackgroundFunction</code> or <code>RawBakgroundFunction</code> interface.
It must find a class in your project that implements one of these interfaces or it will throw a build time failure.
If it finds more than one function classes, a build time exception will also be thrown.</p>
</div>
<div class="paragraph">
<p>Sometimes, though, you might have a few related functions that share code and creating multiple maven modules is just
an overhead you don&#8217;t want to do.  The extension allows you to bundle multiple functions in one
project and use configuration or an environment variable to pick the function you want to deploy.</p>
</div>
<div class="paragraph">
<p>To configure the name of the function, you can use the following configuration property:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">quarkus.google-cloud-functions.function=test</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>quarkus.google-cloud-functions.function</code> property tells Quarkus which function to deploy. This can be overridden
with an environment variable too.</p>
</div>
<div class="paragraph">
<p>The CDI name of the function class must match the value specified within the <code>quarkus.google-cloud-functions.function</code> property.
This must be done using the <code>@Named</code> annotation.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code data-lang="java" class="language-java hljs">@Named("test")
public class TestHttpFunction implements HttpFunction {
}</code></pre>
</div>
</div>
<div class="sect2">
<h3 id="the-httpfunction"><a class="anchor" href="#the-httpfunction"></a>The HttpFunction</h3>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code data-lang="java" class="language-java hljs">import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.Writer;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import io.quarkus.gcp.function.test.service.GreetingService;

@Named("httpFunction") <i class="conum" data-value="1"></i><b>(1)</b>
@ApplicationScoped <i class="conum" data-value="2"></i><b>(2)</b>
public class HttpFunctionTest implements HttpFunction { <i class="conum" data-value="3"></i><b>(3)</b>
    @Inject GreetingService greetingService; <i class="conum" data-value="4"></i><b>(4)</b>

    @Override
    public void service(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception { <i class="conum" data-value="5"></i><b>(5)</b>
        Writer writer = httpResponse.getWriter();
        writer.write(greetingService.hello());
    }
}</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The <code>@Named</code> annotation allows to name the CDI bean to be used by the <code>quarkus.google-cloud-functions.function</code> property, this is optional.</p>
</li>
<li>
<p>The function must be a CDI bean</p>
</li>
<li>
<p>This is a regular Google Cloud Function implementation, so it needs to implement <code>com.google.cloud.functions.HttpFunction</code>.</p>
</li>
<li>
<p>Injection works inside your function.</p>
</li>
<li>
<p>This is standard Google Cloud Function implementation, nothing fancy here.</p>
</li>
</ol>
</div>
</div>
<div class="sect2">
<h3 id="the-backgroundfunction"><a class="anchor" href="#the-backgroundfunction"></a>The BackgroundFunction</h3>
<div class="paragraph">
<p>This <code>BackgroundFunction</code> is triggered by a Storage event, you can use any events supported by Google Cloud instead.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code data-lang="java" class="language-java hljs">import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import com.google.cloud.functions.BackgroundFunction;
import com.google.cloud.functions.Context;
import io.quarkus.gcp.function.test.service.GreetingService;


@Named("storageTest") <i class="conum" data-value="1"></i><b>(1)</b>
@ApplicationScoped <i class="conum" data-value="2"></i><b>(2)</b>
public class BackgroundFunctionStorageTest implements BackgroundFunction&lt;BackgroundFunctionStorageTest.StorageEvent&gt; { <i class="conum" data-value="3"></i><b>(3)</b>
    @Inject GreetingService greetingService;  <i class="conum" data-value="4"></i><b>(4)</b>

    @Override
    public void accept(StorageEvent event, Context context) throws Exception { <i class="conum" data-value="5"></i><b>(5)</b>
        System.out.println("Receive event: " + event);
        System.out.println("Be polite, say " + greetingService.hello());
    }

    //
    public static class StorageEvent { <i class="conum" data-value="6"></i><b>(6)</b>
        public String name;
    }
}</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The <code>@Named</code> annotation allows to name the CDI bean to be used by the <code>quarkus.google-cloud-functions.function</code> property, this is optional.</p>
</li>
<li>
<p>The function must be a CDI bean</p>
</li>
<li>
<p>This is a regular Google Cloud Function implementation, so it needs to implement <code>com.google.cloud.functions.BackgroundFunction</code>.</p>
</li>
<li>
<p>Injection works inside your function.</p>
</li>
<li>
<p>This is standard Google Cloud Function implementation, nothing fancy here.</p>
</li>
<li>
<p>This is the class the event will be deserialized to.</p>
</li>
</ol>
</div>
</div>
<div class="sect2">
<h3 id="the-rawbackgroundfunction"><a class="anchor" href="#the-rawbackgroundfunction"></a>The RawBackgroundFunction</h3>
<div class="paragraph">
<p>This <code>RawBackgroundFunction</code> is triggered by a PubSub event, you can use any events supported by Google Cloud instead.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code data-lang="java" class="language-java hljs">import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import com.google.cloud.functions.Context;
import com.google.cloud.functions.RawBackgroundFunction;
import io.quarkus.gcp.function.test.service.GreetingService;

@Named("rawPubSubTest") <i class="conum" data-value="1"></i><b>(1)</b>
@ApplicationScoped <i class="conum" data-value="2"></i><b>(2)</b>
public class RawBackgroundFunctionPubSubTest implements RawBackgroundFunction { <i class="conum" data-value="3"></i><b>(3)</b>
@Inject GreetingService greetingService; <i class="conum" data-value="4"></i><b>(4)</b>

    @Override
    public void accept(String event, Context context) throws Exception { <i class="conum" data-value="5"></i><b>(5)</b>
        System.out.println("PubSub event: " + event);
        System.out.println("Be polite, say " + greetingService.hello());
    }
}</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>The <code>@Named</code> annotation allows to name the CDI bean to be used by the <code>quarkus.google-cloud-functions.function</code> property, this is optional.</p>
</li>
<li>
<p>The function must be a CDI bean</p>
</li>
<li>
<p>This is a regular Google Cloud Function implementation, so it needs to implement <code>com.google.cloud.functions.RawBackgroundFunction</code>.</p>
</li>
<li>
<p>Injection works inside your function.</p>
</li>
<li>
<p>This is standard Google Cloud Function implementation, nothing fancy here.</p>
</li>
</ol>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="build-and-deploy-to-google-cloud"><a class="anchor" href="#build-and-deploy-to-google-cloud"></a>Build and Deploy to Google Cloud</h2>
<div class="sectionbody">
<div class="paragraph">
<p>To build your application, you can package it using the standard <code>mvn clean package</code> command.</p>
</div>
<div class="paragraph">
<p>The result of the previous command is a single JAR file inside the <code>target/deployment</code> repository that contains classes and dependencies of the project.</p>
</div>
<div class="paragraph">
<p>Then you will be able to use <code>gcloud beta functions deploy</code> command to deploy your function to Google Cloud.</p>
</div>
<div class="admonitionblock warning">
<table>
<tr>
<td class="icon">
<i class="fa icon-warning" title="Warning"></i>
</td>
<td class="content">
<div class="paragraph">
<p>The first time you launch this command, you can have the following error message:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">ERROR: (gcloud.beta.functions.deploy) OperationError: code=7, message=Build Failed: Cloud Build has not been used in project &lt;project_name&gt; before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudbuild.googleapis.com/overview?project=&lt;my-project&gt; then retry.</code></pre>
</div>
</div>
<div class="paragraph">
<p>This means that Cloud Build is not activated yet. To overcome this error, open the URL shown in the error, follow the instructions and then wait a few minutes before retrying the command.</p>
</div>
</td>
</tr>
</table>
</div>
<div class="sect2">
<h3 id="the-httpfunction-2"><a class="anchor" href="#the-httpfunction-2"></a>The HttpFunction</h3>
<div class="paragraph">
<p>This is an example command to deploy your <code>HttpFunction</code> to Google Cloud:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">gcloud beta functions deploy quarkus-example-http \
  --entry-point=io.quarkus.gcp.functions.QuarkusHttpFunction \
  --runtime=java11 --trigger-http --source=target/deployment</code></pre>
</div>
</div>
<div class="admonitionblock important">
<table>
<tr>
<td class="icon">
<i class="fa icon-important" title="Important"></i>
</td>
<td class="content">
<div class="paragraph">
<p>The entry point must always be set to <code>io.quarkus.gcp.functions.QuarkusHttpFunction</code> as this is the class that integrates Cloud Functions with Quarkus.</p>
</div>
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>This command will give you as output a <code>httpsTrigger.url</code> that points to your function.</p>
</div>
</div>
<div class="sect2">
<h3 id="the-backgroundfunction-2"><a class="anchor" href="#the-backgroundfunction-2"></a>The BackgroundFunction</h3>
<div class="paragraph">
<p>Before deploying your function, you need to create a bucket.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">gsutil mb gs://quarkus-hello</code></pre>
</div>
</div>
<div class="paragraph">
<p>This is an example command to deploy your <code>BackgroundFunction</code> to Google Cloud, as the function is triggered by a Storage event,
it needs to use <code>--trigger-event google.storage.object.finalize</code> and the <code>--trigger-resource</code> parameter with the name of a previously created bucket:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">gcloud beta functions deploy quarkus-example-storage \
    --entry-point=io.quarkus.gcp.functions.QuarkusBackgroundFunction \
    --trigger-resource quarkus-hello --trigger-event google.storage.object.finalize \
    --runtime=java11 --source=target/deployment</code></pre>
</div>
</div>
<div class="admonitionblock important">
<table>
<tr>
<td class="icon">
<i class="fa icon-important" title="Important"></i>
</td>
<td class="content">
<div class="paragraph">
<p>The entry point must always be set to <code>io.quarkus.gcp.functions.QuarkusBackgroundFunction</code> as this is the class that integrates Cloud Functions with Quarkus.</p>
</div>
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>To trigger the event, you can send a file to the GCS <code>quarkus-hello</code> bucket or you can use gcloud to simulate one:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">gcloud functions call quarkus-example-storage  --data '{"name":"test.txt"}'</code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<code>--data</code> contains the GCS event, it is a JSON document with the name of the file added to the bucket.
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="the-rawbackgroundfunction-2"><a class="anchor" href="#the-rawbackgroundfunction-2"></a>The RawBackgroundFunction</h3>
<div class="paragraph">
<p>This is an example command to deploy your <code>RawBackgroundFunction</code> to Google Cloud, as the function is triggered by a PubSub event,
it needs to use <code>--trigger-event google.pubsub.topic.publish</code> and the <code>--trigger-resource</code> parameter with the name of a previously created topic:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">gcloud beta functions deploy quarkus-example-pubsub \
  --entry-point=io.quarkus.gcp.functions.QuarkusBackgroundFunction \
  --runtime=java11 --trigger-resource hello_topic --trigger-event google.pubsub.topic.publish --source=target/deployment</code></pre>
</div>
</div>
<div class="admonitionblock important">
<table>
<tr>
<td class="icon">
<i class="fa icon-important" title="Important"></i>
</td>
<td class="content">
<div class="paragraph">
<p>The entry point must always be set to <code>io.quarkus.gcp.functions.QuarkusBackgroundFunction</code> as this is the class that integrates Cloud Functions with Quarkus.</p>
</div>
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>To trigger the event, you can send a file to the <code>hello_topic</code> topic of you can use gcloud to simulate one:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">gcloud functions call quarkus-example-pubsub --data '{"data":{"greeting":"world"}}'</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="testing-locally"><a class="anchor" href="#testing-locally"></a>Testing locally</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The easiest way to locally test your function is using the Cloud Function invoker JAR.</p>
</div>
<div class="paragraph">
<p>You can download it via Maven using the following command:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">mvn dependency:copy \
  -Dartifact='com.google.cloud.functions.invoker:java-function-invoker:1.0.0-beta1' \
  -DoutputDirectory=.</code></pre>
</div>
</div>
<div class="sect2">
<h3 id="the-httpfunction-3"><a class="anchor" href="#the-httpfunction-3"></a>The HttpFunction</h3>
<div class="paragraph">
<p>To test an <code>HttpFunction</code>, you can use this command to launch your function locally.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">java -jar java-function-invoker-1.0.0-beta1.jar \
  --classpath target/google-cloud-functions-1.0-SNAPSHOT-runner.jar \
  --target io.quarkus.gcp.functions.QuarkusHttpFunction</code></pre>
</div>
</div>
<div class="paragraph">
<p>Your endpoints will be available on <a href="http://localhost:8080" class="bare">http://localhost:8080</a>.</p>
</div>
</div>
<div class="sect2">
<h3 id="the-backgroundfunction-3"><a class="anchor" href="#the-backgroundfunction-3"></a>The BackgroundFunction</h3>
<div class="paragraph">
<p>For background functions, you launch the invoker with a target class of <code>io.quarkus.gcp.functions.BackgroundFunction</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">java -jar java-function-invoker-1.0.0-beta1.jar \
  --classpath target/google-cloud-functions-1.0-SNAPSHOT-runner.jar \
  --target io.quarkus.gcp.functions.QuarkusBackgroundFunction</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then you can call your background function via an HTTP call with a payload containing the event:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">curl localhost:8080 -d '{"data":{"name":"hello.txt"}}'</code></pre>
</div>
</div>
<div class="paragraph">
<p>This will call your Storage background function with an event <code>{"name":"hello.txt"}</code>, so an event on the <code>hello.txt</code> file.</p>
</div>
</div>
<div class="sect2">
<h3 id="the-rawbackgroundfunction-3"><a class="anchor" href="#the-rawbackgroundfunction-3"></a>The RawBackgroundFunction</h3>
<div class="paragraph">
<p>For background functions, you launch the invoker with a target class of <code>io.quarkus.gcp.functions.BackgroundFunction</code>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">java -jar java-function-invoker-1.0.0-beta1.jar \
  --classpath target/google-cloud-functions-1.0-SNAPSHOT-runner.jar \
  --target io.quarkus.gcp.functions.QuarkusBackgroundFunction</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then you can call your background function via an HTTP call with a payload containing the event:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-none hljs">curl localhost:8080 -d '{"data":{"greeting":"world"}}'</code></pre>
</div>
</div>
<div class="paragraph">
<p>This will call your PubSub background function with a PubSubMessage <code>{"greeting":"world"}</code>.</p>
</div>
</div>
</div>
</div>
    </div>
  </div>
</div>

  </div>

  <div class="content project-footer">
  <div class="footer-section">
    <div class="logo-wrapper">
      <a href="/"><img src="/assets/images/quarkus_logo_horizontal_rgb_reverse.svg" class="project-logo" title="Quarkus"></a>
    </div>
  </div>
  <div class="grid-wrapper">
    <p class="grid__item width-3-12">Quarkus is open. All dependencies of this project are available under the <a href='https://www.apache.org/licenses/LICENSE-2.0' target='_blank'>Apache Software License 2.0</a> or compatible license.<br /><br />This website was built with <a href='https://jekyllrb.com/' target='_blank'>Jekyll</a>, is hosted on <a href='https://pages.github.com/' target='_blank'>Github Pages</a> and is completely open source. If you want to make it better, <a href='https://github.com/quarkusio/quarkusio.github.io' target='_blank'>fork the website</a> and show us what you’ve got.</p>

    
      <div class="width-1-12 project-links">
        <span>Navigation</span>
        <ul class="footer-links width-1-12">
          
            <li><a href="/">Home</a></li>
          
            <li><a href="/guides">Guides</a></li>
          
            <li><a href="/community/#contributing">Contribute</a></li>
          
            <li><a href="/faq">FAQ</a></li>
          
            <li><a href="/get-started">Get Started</a></li>
          
        </ul>
      </div>
    
      <div class="width-1-12 project-links">
        <span>Contribute</span>
        <ul class="footer-links width-1-12">
          
            <li><a href="https://twitter.com/quarkusio">Follow us</a></li>
          
            <li><a href="https://github.com/quarkusio">GitHub</a></li>
          
            <li><a href="/security">Security&nbsp;policy</a></li>
          
        </ul>
      </div>
    
      <div class="width-1-12 project-links">
        <span>Get Help</span>
        <ul class="footer-links width-1-12">
          
            <li><a href="https://groups.google.com/forum/#!forum/quarkus-dev">Forums</a></li>
          
            <li><a href="https://quarkusio.zulipchat.com">Chatroom</a></li>
          
        </ul>
      </div>
    

    
      <div class="width-3-12 more-links">
        <span>Quarkus is made of community projects</span>
        <ul class="footer-links">
          
            <li><a href="https://vertx.io/" target="_blank">Eclipse Vert.x</a></li>
          
            <li><a href="https://microprofile.io" target="_blank">Eclipse MicroProfile</a></li>
          
            <li><a href="https://hibernate.org" target="_blank">Hibernate</a></li>
          
            <li><a href="https://netty.io" target="_blank">Netty</a></li>
          
            <li><a href="https://resteasy.github.io" target="_blank">RESTEasy</a></li>
          
            <li><a href="https://camel.apache.org" target="_blank">Apache Camel</a></li>
          
            <li><a href="https://code.quarkus.io/" target="_blank">And many more...</a></li>
          
        </ul>
      </div>
    
  </div>
</div>
  <div class="content redhat-footer">
  <div class="grid-wrapper">
    <span class="licence">
      <i class="fab fa-creative-commons"></i><i class="fab fa-creative-commons-by"></i> <a href="https://creativecommons.org/licenses/by/3.0/" target="_blank">CC by 3.0</a> | <a href="https://www.redhat.com/en/about/privacy-policy">Privacy Policy</a>
    </span>
    <span class="redhat">
      Sponsored by
    </span>
    <span class="redhat-logo">
      <a href="https://www.redhat.com/" target="_blank"><img src="/assets/images/redhat_reversed.svg"></a>
    </span>
  </div>
</div>


  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js" integrity="sha384-8gBf6Y4YYq7Jx97PIqmTwLPin4hxIzQw5aDmUg/DDhul9fFpbbLcLh3nTIIDJKhx" crossorigin="anonymous"></script>
  <script type="text/javascript" src="/assets/javascript/mobile-nav.js"></script>
  <script type="text/javascript" src="/assets/javascript/scroll-down.js"></script>
  <script src="/assets/javascript/satellite.js" type="text/javascript"></script>
  <script src="https://quarkus.io/guides/javascript/config.js" type="text/javascript"></script>
  <script src="/assets/javascript/search-filter.js" type="text/javascript"></script>
  <script src="/assets/javascript/back-to-top.js" type="text/javascript"></script>
</body>

</html>
